package com.example.service.impl;

import com.example.context.BaseContext;
import com.example.dto.OrdersSubmitDTO;
import com.example.entity.AddressBook;
import com.example.entity.OrderDetail;
import com.example.entity.Orders;
import com.example.entity.ShoppingCar;
import com.example.exception.AddressBookBusinessException;
import com.example.exception.ShoppingCartBusinessException;
import com.example.mapper.AddressBookMapper;
import com.example.mapper.OrderSubmitDetailMapper;
import com.example.mapper.OrderSubmitMapper;
import com.example.mapper.ShoppingCarMapper;
import com.example.service.OrderSubmitService;
import com.example.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderSubmitServiceImpl implements OrderSubmitService {

    @Autowired
    private OrderSubmitMapper orderSubmitMapper;

    @Autowired
    private OrderSubmitDetailMapper orderSubmitDetailMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @Override
    public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {

        //判断用户地址是否为空
        AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());

        if(addressBook ==null){
            throw  new AddressBookBusinessException("收货地址为空！");
        }
        //判断购物车是否为空
        Long userId = BaseContext.getCurrentId();
        ShoppingCar shoppingCar =new ShoppingCar();
        shoppingCar.setUserId(userId);

        List<ShoppingCar> shoppingCarList = shoppingCarMapper.getShoppingCar(shoppingCar);
        if (shoppingCarList ==null || shoppingCarList.size()==0){
            throw new ShoppingCartBusinessException("购物车为空！");
        }

        //向订单表插入数据
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO, orders);
        orders.setOrderTime(LocalDateTime.now());
        orders.setPayStatus(Orders.UN_PAID);
        orders.setStatus(Orders.PENDING_PAYMENT);
        orders.setNumber(String.valueOf(System.currentTimeMillis()));
        orders.setAddress(addressBook.getDetail());
        orders.setPhone(addressBook.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setUserId(userId);

        orderSubmitMapper.insert(orders);

        List<OrderDetail> orderDetailList = new ArrayList<>();
        //向订单明细表插入n条数据
        for (ShoppingCar car : shoppingCarList) {
            OrderDetail orderDetail = new OrderDetail();//订单明细
            BeanUtils.copyProperties(car, orderDetail);
            orderDetail.setOrderId(orders.getId());//设置当前订单明细关联的订单id
            orderDetailList.add(orderDetail);
        }

        orderSubmitDetailMapper.insertBatch(orderDetailList);

        //清空购物车
        shoppingCarMapper.deleteById(userId);

        //封装返回结果
        OrderSubmitVO build = OrderSubmitVO.builder()
                .id(orders.getId())
                .orderTime(orders.getOrderTime())
                .orderNumber(orders.getNumber())
                .orderAmount(orders.getAmount())
                .build();

        return build;
    }
}
